package com.q.tqb.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.baidu.mapapi.SDKInitializer;
import com.iflytek.cloud.SpeechUtility;

//import com.tencent.bugly.Bugly;
//import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.stat.MtaSDkException;
import com.tencent.stat.StatService;

import java.util.Map;
import java.util.Stack;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by gpw on 2016/10/12.
 * --加油
 */

public class BaseApplication extends MultiDexApplication {
    private static BaseApplication sBaseApplication;
    private Stack<Activity> mActivityStack = new Stack<>();

    public static OSS sOSS;
    //  public static String cooKie;
    // 用于存放倒计时时间
    public static Map<String, Long> map;

    public static BaseApplication getInstance() {
        if (sBaseApplication == null) {
            sBaseApplication = new BaseApplication();
        }
        return sBaseApplication;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(getApplicationContext());
        SpeechUtility.createUtility(this, AppConst.SOUND_APP_ID);
        Bugly.init(getApplicationContext(), "ecd4ac5492", true);

        initOss();


        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush


        //    RongIM.init(this);

        try {
            // 第三个参数必须为：com.tencent.stat.common.StatConstants.VERSION
            StatService.startStatService(this, "A3FP2PZF3C2W", com.tencent.stat.common.StatConstants.VERSION);
        } catch (MtaSDkException e) {
            e.printStackTrace();
        }
    }


    private void initOss() {
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(AppConst.OSS_APP_ID, AppConst.OSS_APP_SECRET);
        ClientConfiguration conf = new ClientConfiguration();
        // 连接超时，默认15秒
        conf.setConnectionTimeout(7 * 1000);
        // socket超时，默认15秒
        conf.setSocketTimeout(7 * 1000);
        // 最大并发请求书，默认5个
        conf.setMaxConcurrentRequest(5);
        // 失败后最大重试次数，默认2次
        conf.setMaxErrorRetry(1);
        sOSS = new OSSClient(getApplicationContext(), "http://oss-cn-shenzhen.aliyuncs.com", credentialProvider, conf);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 安装tinker
        Beta.installTinker();
    }

    public void addActivity(final Activity curAT) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<Activity>();
        }
        mActivityStack.add(curAT);
    }

    public void removeActivity(final Activity curAT) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<Activity>();
        }
        mActivityStack.remove(curAT);
    }


    //返回堆内Activity的总数
    public int howManyActivities() {
        return mActivityStack.size();
    }


    //关闭所有Activity
    public void finishAllActivities() {
        for (int i = 0, size = mActivityStack.size(); i < size; i++) {
            if (null != mActivityStack.get(i)) {
                mActivityStack.get(i).finish();
            }
        }
        mActivityStack.clear();
    }


    public void exit() {
        finishAllActivities();
        //android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}
